#!/usr/bin/env python
# coding: utf-8
# author: Wesley

from django.conf import settings
from django.core.cache import cache
from django.db import models
from lib.common import CacheKeys, BasicManager


class VersionManage(BasicManager):
    def get_app_version_by_os(self, os_type, app_type=1):
        cache_key = CacheKeys.APP_VERSION % (os_type, app_type)
        version = cache.get(cache_key)

        if not version:
            version_values = ["url", "version", "force_upgrade_before_version", "desc"]
            version = self.filter(ostype=os_type, app_type=app_type).values(*version_values).first()

            if version:
                cache.set(cache_key, version, 1800)
            else:
                version = {}

        return version


class Version(models.Model):
    ostype = models.CharField(max_length=1, choices=settings.MOBILE_OS_CHOICES)
    version = models.CharField(max_length=50)
    url = models.URLField(max_length=500)
    force_upgrade_before_version = models.CharField(max_length=50, blank=True, null=True)
    # 强制升级的版本号, 比此版本老的版本需要强制升级
    desc = models.TextField(null=True, blank=True)
    app_type = models.PositiveSmallIntegerField(default=1)  # 1 优势大学app， 目前只有这一种

    objects = VersionManage()

    class Meta:
        db_table = "momself_appversion"


mm_Version = Version.objects
